package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.mapper.BaseTrademarkMapper;
import com.atguigu.gmall.product.model.BaseCategoryTrademark;
import com.atguigu.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.atguigu.gmall.product.model.BaseTrademark;
import com.atguigu.gmall.product.model.CategoryTrademarkVo;
import com.atguigu.gmall.product.service.BaseCategoryTrademarkService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 分类品牌中间表 业务实现类
 *
 * @author atguigu
 * @since 2022-12-24
 */
@Service
public class BaseCategoryTrademarkServiceImpl extends ServiceImpl<BaseCategoryTrademarkMapper, BaseCategoryTrademark> implements BaseCategoryTrademarkService {

    @Autowired
    private BaseTrademarkMapper baseTrademarkMapper;

    /**
     * 查询分类下关联的品牌列表
     *
     * @param category3Id 三级分类ID
     * @return
     */
    @Override
    public List<BaseTrademark> findTrademarkList(Long category3Id) {
        //1.根据分类ID查询 分类品牌关系表得到分类品牌集合
        LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarks = super.list(queryWrapper);

        //2.获取分类品牌集合中品牌ID集合
        if (!CollectionUtils.isEmpty(baseCategoryTrademarks)) {
            //采用Stream流处理集合 将集合对象中 品牌ID 获取  转为品牌ID集合
            List<Long> trademardIdList = baseCategoryTrademarks.stream().map(BaseCategoryTrademark::getTrademarkId).collect(Collectors.toList());
            //3.根据品牌ID集合查询品牌列表
            List<BaseTrademark> baseTrademarkList = baseTrademarkMapper.selectBatchIds(trademardIdList);
            return baseTrademarkList;
        }
        return null;
    }


    /**
     * 查询当前分类可选品牌列表
     *
     * @param category3Id 三级分类ID
     * @return
     */
    @Override
    public List<BaseTrademark> findCurrentTrademarkList(Long category3Id) {
        //调用持久层方法执行自定义SQL得到可选品牌列表
        List<BaseTrademark> list = baseTrademarkMapper.findCurrentTrademarkList(category3Id);
        return list;
    }

    /**
     * 将品牌关联到分类
     *
     * @param categoryTrademarkVo
     * @return
     */
    @Override
    public void saveBasecategoryTrademark(CategoryTrademarkVo categoryTrademarkVo) {
        //1.获取品牌ID集合
        List<Long> trademarkIdList = categoryTrademarkVo.getTrademarkIdList();
        //2.遍历品牌ID集合构建品牌分类中间对象 执行新增
        if (!CollectionUtils.isEmpty(trademarkIdList)) {
            //采用Stream流将集合泛型 由  Long 转为 BaseCategoryTrademark
            List<BaseCategoryTrademark> baseCategoryTrademarkList = trademarkIdList.stream().map(tmId -> {
                BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
                baseCategoryTrademark.setTrademarkId(tmId);
                baseCategoryTrademark.setCategory3Id(categoryTrademarkVo.getCategory3Id());
                return baseCategoryTrademark;
            }).collect(Collectors.toList());
            //执行当前关系业务父类中方法 批量保存
            super.saveBatch(baseCategoryTrademarkList);
        }
    }


    /**
     * 删除分类品牌关联
     *
     * @param category3Id
     * @param trademarkId
     */
    @Override
    public void removeCategoryTrademark(Long category3Id, Long trademarkId) {
        //方式一：逻辑删除
        //LambdaQueryWrapper<BaseCategoryTrademark> queryWrapper = new LambdaQueryWrapper<>();
        //queryWrapper.eq(BaseCategoryTrademark::getCategory3Id, category3Id);
        //queryWrapper.eq(BaseCategoryTrademark::getTrademarkId, trademarkId);
        //super.remove(queryWrapper);
        //方式二：执行物理删除
        baseMapper.removeCategoryTrademark(category3Id, trademarkId);
    }
}
